﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;

namespace MuzoBlog.Core.BussinessLogic.Email
{
    public class MailMessageGenerator
    {
        public MailAddress FromMailAddress { get; set; }

        public MailAddress ReplyToMailAddress { get; set; }

        public List<MailAddress> ToMailAddresses { get; set; }

        public List<MailAddress> BccMailAddresses { get; set; }

        public List<MailAddress> CcMailAddresses { get; set; }

        public AlternateViewCollection AlternateViews { get; set; }

        public LinkedResourceCollection LinkedSources { get; set; }

        public AttachmentCollection Attachments { get; set; }

        public int NumberOfTry { get; set; }

        public string Subject { get; set; }

        public string Body { get; set; }

        public bool IsBodyHtml { get; set; }

        public MailPriority Priority { get; set; }

        public MailMessageGenerator()
        {
            ToMailAddresses = new List<MailAddress>();
            BccMailAddresses = new List<MailAddress>();
            CcMailAddresses = new List<MailAddress>();
            NumberOfTry = 1;
            IsBodyHtml = true;
            Subject = string.Empty;
            Body = string.Empty;
        }

        public MailMessage GetMailMesssage()
        {
            if (FromMailAddress == null)
                throw new ArgumentNullException("FromMailAddress");
            if (ToMailAddresses == null)
                throw new ArgumentNullException("ToMailAddress");

            MailMessage mailMessage = new MailMessage();

            #region FromToBccCc
            mailMessage.From = FromMailAddress;

            foreach (var item in ToMailAddresses)
                mailMessage.To.Add(item);

            foreach (var item in BccMailAddresses)
                mailMessage.Bcc.Add(item);

            foreach (var item in CcMailAddresses)
                mailMessage.CC.Add(item);
            #endregion

            #region Alttachment_AlternateView_LinkedSource

            if (Attachments != null)
            {
                foreach (var item in Attachments)
                    mailMessage.Attachments.Add(item);
            }

            if (AlternateViews != null)
            {
                foreach (var item in AlternateViews)
                {
                    foreach (var linkedSource in item.LinkedResources)
                        item.LinkedResources.Add(linkedSource);

                    mailMessage.AlternateViews.Add(item);
                }
            }

            #endregion

            mailMessage.Subject = Subject;
            mailMessage.Body = Body;
            mailMessage.BodyEncoding = Encoding.UTF8;
            mailMessage.SubjectEncoding = Encoding.UTF8;
            mailMessage.IsBodyHtml = IsBodyHtml;
            mailMessage.Priority = Priority;
            if (ReplyToMailAddress != null)
                mailMessage.ReplyTo = ReplyToMailAddress;

            return mailMessage;
        }
    }
}